<?php

namespace App\Models;

use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Zizaco\Entrust\EntrustPermission;


class Permission extends EntrustPermission implements Transformable
{
    use TransformableTrait;

    protected $table = 'permissions';
    //批量赋值
    protected $fillable = [
        'id',
        'namespace',
        'controller',
        'action',
        'name',
        'status',
        'icon',
        'sort',
    ];

    protected $appends = ['icon_html', 'sub_permission'];

    public function getIconHtmlAttribute()
    {
        return $this->attributes['icon'] ? '<i class="mdi ' . $this->attributes['icon'] . '"></i>' : '';
    }

    public function getSubPermissionAttribute()
    {
        return $this->attributes['namespace'] ? $this->where([
            ['namespace', $this->attributes['namespace']],
            ['controller', '!=', ''],
            ['action', '!=', ''],
        ])->orderBy('sort', 'asc')->get() : null;
    }
}
